Fix: Support media extraction from templateMessage in getBase64FromMediaMessage#1715
Merged
DavidsonGomes merged 1 commit intoEvolutionAPI:developfrom Jul 14, 2025
Conversation
…diaMessage
### Fix: Add support for templateMessage media in getBase64FromMediaMessage
#### What this does
Adds support to download media from `templateMessage` structures in `getBase64FromMediaMessage`, by checking for `hydratedTemplate` and `hydratedFourRowTemplate`.
#### Why it's needed
Currently, media inside templates (e.g. `imageMessage`, `videoMessage`, `documentMessage`) is not processed by the method, which leads to errors or media being skipped.
#### How it works
If a `templateMessage` is detected, the code looks into the inner hydrated template and assigns the correct `mediaMessage` and `mediaType`. Then it proceeds as usual with the download logic.
#### Example message
```json
{
"message": {
"templateMessage": {
"hydratedTemplate": {
"imageMessage": {
"mimetype": "image/jpeg",
"fileLength": 123456,
"url": "https://..."
}
}
}
}
}
Contributor
Reviewer's GuideEnhance media extraction in getBase64FromMediaMessage by detecting and normalizing media embedded within templateMessage structures before falling back to the existing logic. Sequence diagram for media extraction from templateMessage in getBase64FromMediaMessagesequenceDiagram
participant Caller
participant BaileysService
participant Template
Caller->>BaileysService: getBase64FromMediaMessage(msg)
alt msg contains templateMessage
BaileysService->>Template: Access hydratedTemplate or hydratedFourRowTemplate
alt Template contains supported media type
BaileysService->>BaileysService: Extract mediaMessage and mediaType
BaileysService->>BaileysService: Normalize msg.message
else Template does not contain supported media
BaileysService->>Caller: Throw error
end
else msg does not contain templateMessage
BaileysService->>BaileysService: Fallback to existing media extraction logic
alt No supported media type found
BaileysService->>Caller: Throw error
end
end
BaileysService-->>Caller: Return base64 media or error
Class diagram for updated media extraction logic in BaileysStartupServiceclassDiagram
class BaileysStartupService {
+getBase64FromMediaMessage(msg)
}
class Message {
+templateMessage
+imageMessage
+videoMessage
+documentMessage
}
class TemplateMessage {
+hydratedTemplate
+hydratedFourRowTemplate
}
class HydratedTemplate {
+imageMessage
+videoMessage
+documentMessage
}
BaileysStartupService --> Message
Message --> TemplateMessage
TemplateMessage --> HydratedTemplate
HydratedTemplate --> Message
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey @AlexisJusviack - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:3441` </location>
<code_context>
- mediaType = type;
- break;
+ if (msg.message?.templateMessage) {
+ const template =
+ msg.message.templateMessage.hydratedTemplate || msg.message.templateMessage.hydratedFourRowTemplate;
+
</code_context>
<issue_to_address>
Potential edge case if both hydratedTemplate and hydratedFourRowTemplate are undefined.
Accessing template[type] without verifying that template is defined may cause a runtime error. Please add a check to ensure template is defined before using it.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| mediaType = type; | ||
| break; | ||
| if (msg.message?.templateMessage) { | ||
| const template = |
Contributor
There was a problem hiding this comment.
issue: Potential edge case if both hydratedTemplate and hydratedFourRowTemplate are undefined.
Accessing template[type] without verifying that template is defined may cause a runtime error. Please add a check to ensure template is defined before using it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Add support for templateMessage media in getBase64FromMediaMessage
What this does
Adds support to download media from
templateMessagestructures ingetBase64FromMediaMessage, by checking forhydratedTemplateandhydratedFourRowTemplate.Why it's needed
Currently, media inside templates (e.g.
imageMessage,videoMessage,documentMessage) is not processed by the method, which leads to errors or media being skipped.How it works
If a
templateMessageis detected, the code looks into the inner hydrated template and assigns the correctmediaMessageandmediaType. Then it proceeds as usual with the download logic.Example message
{ "message": { "templateMessage": { "hydratedTemplate": { "imageMessage": { "mimetype": "image/jpeg", "fileLength": 123456, "url": "https://..." } } } } } ## Summary by Sourcery Support media extraction from templateMessage structures in getBase64FromMediaMessage Bug Fixes: - Process media inside hydratedTemplate and hydratedFourRowTemplate to correctly retrieve mediaMessage and mediaType from templateMessage Enhancements: - Throw an explicit error when a templateMessage lacks a supported media type